package com.apptest.android.ui.ArticleList;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.apptest.android.core.Constants;
import com.apptest.android.core.bean.ArticleItemBean;
import com.apptest.android.core.bean.ArticleListBean;
import com.apptest.android.core.network.ApiEngine;
import com.apptest.android.core.network.BaseObserver;

import java.util.List;

public class ArticleListViewModel extends ViewModel {
    private MutableLiveData<List<ArticleItemBean>> articleItemList;
    private int articleSource;
    private int id;
    private int currentPage;

    public ArticleListViewModel(){
        articleItemList = new MutableLiveData<>();
    }

    public LiveData<List<ArticleItemBean>> getArticleItemList() {
        return articleItemList;
    }

    public void updateArticleList(boolean refresh){
        if(refresh){
            currentPage = 0;
        }else {
            currentPage++;
        }
        switch (articleSource){
            case Constants.ARTICLE_FROM_WECHAT:
                ApiEngine.getWxArticles(id, currentPage, new BaseObserver<ArticleListBean>() {
                    @Override
                    public void onSuccess(ArticleListBean articleListBean) {
                        articleItemList.setValue(articleListBean.getDatas());
                    }
                });
                break;
            case Constants.ARTICLE_FROM_ARCHITECTURE:
                ApiEngine.getArchitectureArticles(id, currentPage, new BaseObserver<ArticleListBean>() {
                    @Override
                    public void onSuccess(ArticleListBean articleListBean) {
                        articleItemList.setValue(articleListBean.getDatas());
                    }
                });
                break;
            default:
                break;
        }
    }

    public void setArticleListSource(int source, int id){
        this.articleSource = source;
        this.id = id;
    }

    public void setArticleItemList(List<ArticleItemBean> list){
        articleItemList.setValue(list);
    }
}
